using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

namespace Weather
{
    public class MainForm : Form
    {
        private Panel navigationPanel;
        private Panel contentPanel;
        private Panel activeIndicator;
        private Label lblUserInfo;
        private List<Button> navButtons;

        public MainForm()
        {
            InitializeComponents();
            CustomizeDesign();
        }

        private void InitializeComponents()
        {
            this.Size = new Size(1000, 600);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "心情日记";
            this.BackColor = Color.FromArgb(245, 246, 247);

            // 导航面板
            navigationPanel = new Panel
            {
                Dock = DockStyle.Left,
                Width = 220,
                BackColor = Color.FromArgb(44, 43, 60)
            };

            // 用户信息显示
            lblUserInfo = new Label
            {
                Text = $"欢迎, {CurrentUser.Username}",
                ForeColor = Color.White,
                Font = new Font("Microsoft YaHei", 12),
                Location = new Point(20, 20),
                Size = new Size(180, 30),
                TextAlign = ContentAlignment.MiddleLeft
            };
            navigationPanel.Controls.Add(lblUserInfo);

            // 活动指示器
            activeIndicator = new Panel
            {
                Size = new Size(4, 40),
                BackColor = Color.FromArgb(72, 190, 117),
                Location = new Point(0, 70),
                Visible = true
            };
            navigationPanel.Controls.Add(activeIndicator);

            navButtons = new List<Button>();
            
            // 导航按钮
            CreateNavButton("记录心情", "📝", 0);
            CreateNavButton("天气查询", "🌤", 1);
            CreateNavButton("心情历史", "📊", 2);
            CreateNavButton("心情分析", "📈", 3);

            // 内容面板
            contentPanel = new Panel
            {
                Dock = DockStyle.Fill,
                BackColor = Color.White,
                Padding = new Padding(20)
            };

            // 添加圆角和阴影效果
            contentPanel.Paint += (s, e) =>
            {
                using (var path = GetRoundedRectPath(contentPanel.ClientRectangle, 10))
                {
                    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    using (var brush = new SolidBrush(Color.White))
                    {
                        e.Graphics.FillPath(brush, path);
                    }
                }
            };

            this.Controls.Add(contentPanel);
            this.Controls.Add(navigationPanel);

            // 默认显示记录心情页面
            ShowForm(new NewMoodForm());
        }

        private void CustomizeDesign()
        {
            // 设置窗体样式
            this.FormBorderStyle = FormBorderStyle.None;
            this.DoubleBuffered = true;

            // 添加自定义标题栏
            Panel titleBar = new Panel
            {
                Dock = DockStyle.Top,
                Height = 30,
                BackColor = Color.FromArgb(44, 43, 60)
            };

            Label lblTitle = new Label
            {
                Text = "心情日记",
                ForeColor = Color.White,
                Font = new Font("Microsoft YaHei", 10),
                Location = new Point(10, 5),
                AutoSize = true
            };

            Button btnClose = new Button
            {
                Text = "×",
                Size = new Size(30, 30),
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.White,
                Dock = DockStyle.Right,
                FlatAppearance = { BorderSize = 0 }
            };
            btnClose.Click += (s, e) => Application.Exit();

            Button btnMinimize = new Button
            {
                Text = "─",
                Size = new Size(30, 30),
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.White,
                Dock = DockStyle.Right,
                FlatAppearance = { BorderSize = 0 }
            };
            btnMinimize.Click += (s, e) => this.WindowState = FormWindowState.Minimized;

            titleBar.Controls.AddRange(new Control[] { lblTitle, btnClose, btnMinimize });
            this.Controls.Add(titleBar);

            // 添加拖动功能
            titleBar.MouseDown += (s, e) =>
            {
                if (e.Button == MouseButtons.Left)
                {
                    ReleaseCapture();
                    SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
                }
            };
        }

        private Button CreateNavButton(string text, string icon, int index)
        {
            Button button = new Button
            {
                Text = $"{icon} {text}",
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.White,
                Font = new Font("Microsoft YaHei", 10),
                Size = new Size(220, 40),
                Location = new Point(0, 70 + 50 * index),
                TextAlign = ContentAlignment.MiddleLeft,
                Padding = new Padding(20, 0, 0, 0),
                BackColor = Color.FromArgb(44, 43, 60)
            };

            button.FlatAppearance.BorderSize = 0;
            button.FlatAppearance.MouseOverBackColor = Color.FromArgb(64, 63, 80);
            button.FlatAppearance.MouseDownBackColor = Color.FromArgb(72, 190, 117);

            button.Click += NavButton_Click;
            navigationPanel.Controls.Add(button);
            navButtons.Add(button);
            return button;
        }

        private void NavButton_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            activeIndicator.Location = new Point(0, btn.Location.Y);

            // 重置所有按钮颜色
            foreach (var navBtn in navButtons)
            {
                navBtn.BackColor = Color.FromArgb(44, 43, 60);
            }

            // 设置选中按钮颜色
            btn.BackColor = Color.FromArgb(64, 63, 80);

            contentPanel.Controls.Clear();

            switch (btn.Text.Substring(3)) // 跳过emoji
            {
                case "记录心情":
                    ShowForm(new NewMoodForm());
                    break;
                case "天气查询":
                    ShowForm(new Weather.WeatherForm());
                    break;
                case "心情历史":
                    ShowForm(new Weather.MoodHistoryForm());
                    break;
                case "心情分析":
                    ShowForm(new Weather.MoodAnalysisForm());
                    break;
            }
        }

        private void ShowForm(Form form)
        {
            contentPanel.Controls.Clear();
            form.TopLevel = false;
            form.FormBorderStyle = FormBorderStyle.None;
            form.Dock = DockStyle.Fill;
            contentPanel.Controls.Add(form);
            form.Show();
        }

        private System.Drawing.Drawing2D.GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            var path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddArc(rect.X, rect.Y, radius * 2, radius * 2, 180, 90);
            path.AddArc(rect.Right - radius * 2, rect.Y, radius * 2, radius * 2, 270, 90);
            path.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
            path.AddArc(rect.X, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
            path.CloseFigure();
            return path;
        }

        // 用于窗体拖动
        private const int WM_NCLBUTTONDOWN = 0xA1;
        private const int HT_CAPTION = 0x2;

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImport("user32.dll")]
        private static extern bool ReleaseCapture();
    }
} 